﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace App20._5
{
    /// <summary>
    /// PieGraphControl.xaml 的交互逻辑
    /// </summary>
    public partial class PieGraphControl : UserControl
    {
        private Point origin = new Point(100, 100);
        private int radius = 100;
        private double percentageTotal = 0;
        private double initialAngle = 0;
        private List<PiePieceData> piePieces = new List<PiePieceData>();
        private Brush[] brushArray = new Brush[]
        {
            Brushes.Aquamarine,
            Brushes.Azure,
            Brushes.Blue,
            Brushes.Chocolate,
            Brushes.Crimson,
            Brushes.DarkGreen,
            Brushes.DarkGray,
            Brushes.DarkSlateBlue,
            Brushes.Maroon,
            Brushes.Teal,
            Brushes.Violet
        };

        public PieGraphControl()
        {
            InitializeComponent();
            this.DataContext = piePieces;
        }

        private double ConvertToRadians(double theta)
        {
            return (Math.PI / 180) * theta;
        }

        private double CalcAngleFromPercentage(double percentage)
        {
            return 360 * percentage;
        }

        private Point CreatePointFromAngle(double angleInRadians)
        {
            Point p = new Point();

            p.X = radius * Math.Cos(angleInRadians) + origin.X;
            p.Y = radius * Math.Sin(angleInRadians) + origin.Y;

            return p;
        }

        private PathFigure CreatePiePiece(PiePieceData pieceData)
        {
            PathFigure piePiece = new PathFigure();
            piePiece.StartPoint = origin;

            //StartLine
            piePiece.Segments.Add(new LineSegment(pieceData.InitialSide, true));
            //Arc
            piePiece.Segments.Add(new ArcSegment(pieceData.TerminalSide,
                new Size(radius, radius),
                0,
                pieceData.IsCreaterThan180,
                SweepDirection.Clockwise,
                true));
            //EndLine
            piePiece.Segments.Add(new LineSegment(pieceData.TerminalSide, true));

            return piePiece;
        }

        public void AddPiePiece(double percentage, string label)
        {
            if (percentageTotal + percentage > 1.0)
            {
                throw new Exception("Cannot add percentage.Will make total greater than 100%");
            }

            PiePieceData pieceData = new PiePieceData();
            pieceData.Percentage = percentage;
            pieceData.Label = label;

            //Initial and Terminal sides
            double angle = CalcAngleFromPercentage(percentage);
            double endAngle = initialAngle + angle;
            double thetaInit = ConvertToRadians(initialAngle);
            double thetaEnd = ConvertToRadians(endAngle);

            pieceData.InitialSide = CreatePointFromAngle(thetaInit);
            pieceData.TerminalSide = CreatePointFromAngle(thetaEnd);

            initialAngle = endAngle;

            piePieces.Add(pieceData);
        }

        public void RenderGraph()
        {
            int i = 0;

            foreach (PiePieceData piePiece in piePieces)
            {
                PathGeometry pieGeometry = new PathGeometry();
                pieGeometry.Figures.Add(CreatePiePiece(piePiece));

                Path path = new Path();
                path.Data = pieGeometry;
                path.Fill = brushArray[i++ % brushArray.Length];
                piePiece.Color = (Brush)brushArray[i++ % brushArray.Length];
                canvas1.Children.Add(path);
            }
        }
    }

    public class PiePieceData
    {
        private double percentage;
        private string label;
        private bool isCreaterThan180;
        private Point initialSide;
        private Point terminalSide;
        private Brush color;

        public Brush Color
        {
            get { return color; }
            set { color = value; }
        }

        public double Percentage
        {
            get { return percentage; }
            set { percentage = value; }
        }

        public string Label
        {
            get { return label; }
            set { label = value; }
        }

        public bool IsCreaterThan180
        {
            get { return isCreaterThan180; }
            set { isCreaterThan180 = value; }
        }

        public Point InitialSide
        {
            get { return initialSide; }
            set { initialSide = value; }
        }

        public Point TerminalSide
        {
            get { return terminalSide; }
            set { terminalSide = value; }
        }

        public PiePieceData()
        {
        }

        public PiePieceData(double percentage, string label)
        {
            this.percentage = percentage;
            this.label = label;
        }

        
    }
}
